package com.manor.main.fragment

import android.content.Context
import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.manor.common.utils.DpUtil
import com.manor.common.utils.WordUtil
import com.manor.main.R
import com.manor.main.adapter.CommonPagerAdapter
import kotlinx.android.synthetic.main.view_main_vulgar_page.*
import net.lucode.hackware.magicindicator.ViewPagerHelper
import net.lucode.hackware.magicindicator.buildins.UIUtil
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView

/**
 * 魅力榜
 */
class CharmFragment(var type: Int) : Fragment() {

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        var view:View = inflater.inflate(R.layout.view_main_vulgar_page, container, false)

        return view
    }
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val DAY = "day"
        val WEEK = "week"
        val MONTH = "month"
        val TOTAL = "total"
        val titles = arrayOf(WordUtil.getString(R.string.day_rank), WordUtil.getString(R.string.week_rank), WordUtil.getString(R.string.month_rank), WordUtil.getString(R.string.sum_rank))

        val day_rank = VulgarItemFragment(DAY,type)
        val week_rank = VulgarItemFragment(WEEK,type)
        val month_rank = VulgarItemFragment(MONTH,type)
        val sum_rank = VulgarItemFragment(TOTAL,type)
        val pagerAdapter = CommonPagerAdapter(childFragmentManager, day_rank, week_rank, month_rank, sum_rank)

        view_pager?.offscreenPageLimit = 0//加载几页
        view_pager?.adapter = pagerAdapter
        val commonNavigator = CommonNavigator(activity)
        commonNavigator.isAdjustMode = true
        commonNavigator.adapter = object : CommonNavigatorAdapter() {

            override fun getCount(): Int {
                return titles.size
            }

            override fun getTitleView(context: Context, index: Int): IPagerTitleView {
                val simplePagerTitleView = ColorTransitionPagerTitleView(context)
                simplePagerTitleView.normalColor = Color.parseColor("#80e5e5e5")
                simplePagerTitleView.selectedColor = Color.parseColor("#e5e5e5")
                simplePagerTitleView.text = titles[index]
                simplePagerTitleView.textSize = 14f
                simplePagerTitleView.paint.isFakeBoldText = true
                simplePagerTitleView.setPadding(DpUtil.dp2px(20), 0, DpUtil.dp2px(20), 0)
                simplePagerTitleView.setOnClickListener {
                    if (view_pager != null) {
                        view_pager?.currentItem = index
                    }
                }
                return simplePagerTitleView
            }

            override fun getIndicator(context: Context): IPagerIndicator {
                val indicator = LinePagerIndicator(context)
                indicator.setColors(Color.parseColor("#FFC257"))
                indicator.lineWidth = UIUtil.dip2px(context, 30.0).toFloat()
                indicator.mode = LinePagerIndicator.MODE_EXACTLY
                return indicator
            }
        }
        magic_indicator.navigator = commonNavigator
        ViewPagerHelper.bind(magic_indicator, view_pager!!)
        view_pager?.currentItem = 0
    }

}
